package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.InfraredCmdDeal;
import com.zhuoyue.common.constant.InfraredDeviceConstant;
import com.zhuoyue.common.utils.CmdUtils;
import com.zhuoyue.common.utils.DataTypeUtil;
/**
 * @ClassName: Infrared
 * @Description:
 * @author wjming
 * @Date 2021-07-14 20:18
 */
public class Infrared implements Device {

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");

        String cmd = "";
        if (InfraredDeviceConstant.InfraredDealType.INFRARED_STUDY.equals(controlType)) {
            cmd = getInfraredStudyCmd(map);
        } else if (InfraredDeviceConstant.InfraredDealType.INFRARED_SEND.equals(controlType)) {
            cmd = getInfraredSendCmd(map);
        } else if (InfraredDeviceConstant.InfraredDealType.AC_SWITCH_SEND.equals(controlType)) {
            cmd = getAcSwitchSendCmd(map);
        } else if (InfraredDeviceConstant.InfraredDealType.AC_MODE_SEND.equals(controlType)) {
            cmd = getAcModeSendCmd(map);
        } else if (InfraredDeviceConstant.InfraredDealType.AC_TEMP_SEND.equals(controlType)) {
            cmd = getAcTempSendCmd(map);
        } else if (InfraredDeviceConstant.InfraredDealType.AC_WIND_SPEED_SEND.equals(controlType)) {
            cmd = getAcWindSpeedSendCmd(map);
        } else if (InfraredDeviceConstant.InfraredDealType.AC_WIND_DIRECTION_SEND.equals(controlType)) {
            cmd = getAcWindDirectionSendCmd(map);
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_OPEN.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_CLOSE.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_COOL.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_DEHUMIDIFY.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_AIR_SUPPLY.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_HOT.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_WIND_SPEED_HIGHT.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_WIND_SPEED_MIDDLE.equals(controlType) ||
                InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_WIND_SPEED_LOW.equals(controlType)) {
            cmd = getCentralAcFixedCmd(map);
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_TEMP.equals(controlType)) {
            cmd = getCentralAcTempCmd(map);
        }

        return cmd;
    }

    private String getCentralAcTempCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String centralAcCode = actionsMap.get("centralAcCode");
        String temp = actionsMap.get("temp");

        String controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_TEMP;

        String centralAcTempCmd = controlCmd +
                CmdUtils.intToHexTwo(Integer.parseInt(temp)) +
                "01" +
                centralAcCode +
                CmdUtils.getCmdBodySum(controlCmd +
                        CmdUtils.intToHexTwo(Integer.parseInt(temp)) +
                        "01" +
                        centralAcCode);

        String fullCmd = getCommonCmd(centralAcTempCmd, map);

        return fullCmd;

    }


    /**
     * 获取中央空调固定控制命令
     *
     * @param map
     * @return
     */
    private String getCentralAcFixedCmd(Map<String, String> map) {

        String controlType = map.get("controlType");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String centralAcCode = actionsMap.get("centralAcCode");

        String controlCmd = "";
        if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_OPEN.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_OPEN;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_CLOSE.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_CLOSE;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_COOL.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_MODE_COOL;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_DEHUMIDIFY.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_MODE_DEHUMIDIFY;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_AIR_SUPPLY.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_MODE_AIR_SUPPLY;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_MODE_HOT.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_MODE_HOT;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_WIND_SPEED_HIGHT.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_WIND_SPEED_HIGHT;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_WIND_SPEED_MIDDLE.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_WIND_SPEED_MIDDLE;
        } else if (InfraredDeviceConstant.CentralAcControlType.CENTRAL_AC_WIND_SPEED_LOW.equals(controlType)) {
            controlCmd = InfraredCmdDeal.CentralAcFixedControlValue.CENTRAL_AC_WIND_SPEED_LOW;
        }

        String centralAcFixedCmd = controlCmd +
                centralAcCode +
                CmdUtils.getCmdBodySum(controlCmd + centralAcCode);

        String fullCmd = getCommonCmd(centralAcFixedCmd, map);

        return fullCmd;
    }


    private String getAcWindDirectionSendCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String windDirection = actionsMap.get("windDirection");

        String acWindDirectionCmd1To4 = InfraredCmdDeal.AcWindDirectionSend.CMD_1 +
                windDirection +
                InfraredCmdDeal.AcWindDirectionSend.CMD_3 +
                InfraredCmdDeal.AcWindDirectionSend.CMD_4;

        String acWindDirectionCmd5 = DataTypeUtil.getAddCheckByXor(acWindDirectionCmd1To4);

        String acWindDirectionCmd = acWindDirectionCmd1To4 + acWindDirectionCmd5;

        String fullCmd = getCommonCmd(acWindDirectionCmd, map);

        return fullCmd;

    }


    private String getAcWindSpeedSendCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String windSpeed = actionsMap.get("windSpeed");

        String acWindSpeedCmd1To4 = InfraredCmdDeal.AcWindSpeedSend.CMD_1 +
                windSpeed +
                InfraredCmdDeal.AcWindSpeedSend.CMD_3 +
                InfraredCmdDeal.AcWindSpeedSend.CMD_4;

        String acWindSpeedCmd5 = DataTypeUtil.getAddCheckByXor(acWindSpeedCmd1To4);

        String acWindSpeedCmd = acWindSpeedCmd1To4 + acWindSpeedCmd5;

        String fullCmd = getCommonCmd(acWindSpeedCmd, map);

        return fullCmd;

    }


    private String getAcTempSendCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String temp = actionsMap.get("temp");

        String acTempSendCmd2 = CmdUtils.intToHexTwo(Integer.parseInt(temp));

        String acTempSendCmd1To4 = InfraredCmdDeal.AcTempSend.CMD_1 +
                acTempSendCmd2 +
                InfraredCmdDeal.AcTempSend.CMD_3 +
                InfraredCmdDeal.AcTempSend.CMD_4;

        String acTempSendCmd5 = DataTypeUtil.getAddCheckByXor(acTempSendCmd1To4);

        String acTempSendCmd = acTempSendCmd1To4 + acTempSendCmd5;

        String fullCmd = getCommonCmd(acTempSendCmd, map);

        return fullCmd;

    }


    private String getAcModeSendCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String mode = actionsMap.get("mode");

        String acModeSendCmd1To4 = InfraredCmdDeal.AcModeSend.CMD_1 +
                mode +
                InfraredCmdDeal.AcModeSend.CMD_3 +
                InfraredCmdDeal.AcModeSend.CMD_4;

        String acModeSendCmd5 = DataTypeUtil.getAddCheckByXor(acModeSendCmd1To4);

        String acModeSendCmd = acModeSendCmd1To4 + acModeSendCmd5;

        String fullCmd = getCommonCmd(acModeSendCmd, map);

        return fullCmd;

    }


    private String getAcSwitchSendCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String switchState = actionsMap.get("switchState");

        String acSwitchSendCmd1To4 = InfraredCmdDeal.AcSwitchSend.CMD_1 +
                switchState +
                InfraredCmdDeal.AcSwitchSend.CMD_3 +
                InfraredCmdDeal.AcSwitchSend.CMD_4;

        String acSwitchSendCmd5 = DataTypeUtil.getAddCheckByXor(acSwitchSendCmd1To4);

        String acSwitchSendCmd = acSwitchSendCmd1To4 + acSwitchSendCmd5;

        String fullCmd = getCommonCmd(acSwitchSendCmd, map);

        return fullCmd;
    }


    private String getInfraredStudyCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        //学习按钮的编号
        String studyNumber = actionsMap.get("studyNumber");

        String infraredStudyCmd2 = CmdUtils.intToHexTwo(Integer.parseInt(studyNumber));

        String infraredStudyCmd1To4 = InfraredCmdDeal.InfraredStudy.CMD_1 +
                infraredStudyCmd2 +
                InfraredCmdDeal.InfraredStudy.CMD_3 +
                InfraredCmdDeal.InfraredStudy.CMD_4;

        String infraredStudyCmd5 = DataTypeUtil.getAddCheckByXor(infraredStudyCmd1To4);

        String infraredStudyCmd = infraredStudyCmd1To4 + infraredStudyCmd5;

        String fullCmd = getCommonCmd(infraredStudyCmd, map);

        return fullCmd;
    }


    private String getInfraredSendCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        //学习按钮的编号
        String studyNumber = actionsMap.get("studyNumber");

        String infraredSendCmd2 = CmdUtils.intToHexTwo(Integer.parseInt(studyNumber));

        String infraredSendCmd1To4 = InfraredCmdDeal.InfraredSend.CMD_1 +
                infraredSendCmd2 +
                InfraredCmdDeal.InfraredSend.CMD_3 +
                InfraredCmdDeal.InfraredSend.CMD_4;

        String infraredSendCmd5 = DataTypeUtil.getAddCheckByXor(infraredSendCmd1To4);

        String infraredSendCmd = infraredSendCmd1To4 + infraredSendCmd5;

        String fullCmd = getCommonCmd(infraredSendCmd, map);

        return fullCmd;

    }


    /**
     * 处理命令的公共部分
     * @param infraredCmd
     * @param map
     * @return
     */
    private String getCommonCmd(String infraredCmd, Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");

        String infraredCommonCmd7 = CmdUtils.getDateLength(infraredCmd);

        String infraredCommonCmd3To8 = InfraredCmdDeal.InfraredCommon.CMD_3 +
                InfraredCmdDeal.InfraredCommon.CMD_4 +
                gatewayMac +
                deviceMac +
                infraredCommonCmd7 +
                infraredCmd;

        String infraredCommonCmd2 = CmdUtils.getDateLength(infraredCommonCmd3To8);

        String infraredCommonCmd9 = CmdUtils.getCmdBodySum(infraredCommonCmd3To8);

        String fullCmd = InfraredCmdDeal.InfraredCommon.CMD_1 +
                infraredCommonCmd2 +
                infraredCommonCmd3To8 +
                infraredCommonCmd9 +
                InfraredCmdDeal.InfraredCommon.CMD_10;

        return fullCmd;

    }


}
